//
//  OperateCell.m
//  zhijian
//
//  Created by yiban on 2019/1/22.
//  Copyright © 2019 lcb. All rights reserved.
//

#import "OperateCell.h"
#import "SDAutoLayout.h"
#import "QuestionModel.h"
@interface OperateCell ()
{
   
}
@property (strong, nonatomic) UIButton *commonBtn;
@property (strong, nonatomic) UIButton *likeBtn;

@end
@implementation OperateCell
- (instancetype)init
{
    if (self = [super init]) {
    
        [self setup];
    }
    return self;
}
-(void)setup{

    //
    _commonBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    _commonBtn.titleLabel.font = [UIFont systemFontOfSize:14];
    [_commonBtn setTitleColor:UIColorOfHex(0x999999) forState:UIControlStateNormal];
    _commonBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
    [_commonBtn setImage:[UIImage imageNamed:@"comment_nor"] forState:UIControlStateNormal];
    [_commonBtn setTitle:@"评论" forState:UIControlStateNormal];
    [_commonBtn addTarget:self action:@selector(clickCmtEvent) forControlEvents:UIControlEventTouchUpInside];
    //
    _likeBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    _likeBtn.titleLabel.font = [UIFont systemFontOfSize:14];
    [_likeBtn setTitleColor:UIColorOfHex(0x999999) forState:UIControlStateNormal];
    _likeBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
    [_likeBtn setTitle:@"收藏" forState:UIControlStateNormal];
    [_likeBtn setImage:[UIImage imageNamed:@"like_nor"] forState:UIControlStateNormal];
    [_likeBtn addTarget:self action:@selector(clickLikeEvent) forControlEvents:UIControlEventTouchUpInside];
    
    [self sd_addSubviews:@[_commonBtn,_likeBtn]];
    self.sd_equalWidthSubviews = @[_commonBtn,_likeBtn];
    
    // 设置约束
    CGFloat margin = 5;
    //
    _likeBtn.sd_layout
    .rightSpaceToView(self, margin)
    .topSpaceToView(self,margin)
    .widthIs((SCREEN_WIDTH/2)/3)
    .heightIs(20);
    
    _commonBtn.sd_layout
    .rightSpaceToView(_likeBtn, margin)
    .topSpaceToView(self,margin)
    .widthIs((SCREEN_WIDTH/2)/3)
    .heightIs(20);

}
-(void)clickCmtEvent
{
    if (self.commentClickedOperation) {
        self.commentClickedOperation(self.baseModel);
    }
}
-(void)clickLikeEvent
{
    if ([BaseShare sharedBaseShare].isLogin) {
        QuestionModel *qModel = (QuestionModel *)self.baseModel;
        if (qModel.isCollect==1) {
            __weak __typeof(self)weakSelf = self;
            [[Api sharedApi] collectioncancle:[NSString stringWithFormat:@"%ld",qModel.questionId] callback:^(ApiResponse *response,NSString* errorMsg) {
                [BaseToast show:response.message];
                qModel.isCollect = 0;
                [weakSelf.likeBtn setImage:[UIImage imageNamed:@"like_nor"] forState:UIControlStateNormal];
            }];
        }else{
            __weak __typeof(self)weakSelf = self;
            [[Api sharedApi] collectionQuestion:[NSString stringWithFormat:@"%ld",qModel.questionId] callback:^(ApiResponse *response,NSString* errorMsg) {
                [BaseToast show:response.message];
                qModel.isCollect = 1;
                [weakSelf.likeBtn setImage:[UIImage imageNamed:@"like_sel"] forState:UIControlStateNormal];
            }];
        }
    }else{
        [BaseToast show:@"未登录！"];
    }
    
   
}
-(void)setBaseModel:(BaseModel *)baseModel
{
    _baseModel = baseModel;
    NSLog(@"operate -----setZhiJianModel");
    QuestionModel *question = (QuestionModel*)baseModel;
    if (question.isCollect>0) {
        [_likeBtn setImage:[UIImage imageNamed:@"like_sel"] forState:UIControlStateNormal];
    }
//    [self updateLayout];
}

@end
